home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / ImageEnhance.py < prev    next >
Encoding:
Python Source  |  2009-06-16  |  2.7 KB  |  91 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # image enhancement classes
  6. #
  7. # For a background, see "Image Processing By Interpolation and
  8. # Extrapolation", Paul Haeberli and Douglas Voorhies.  Available
  9. # at http://www.sgi.com/grafica/interp/index.html
  10. #
  11. # History:
  12. # 1996-03-23 fl  Created
  13. # 2009-06-16 fl  Fixed mean calculation
  14. #
  15. # Copyright (c) Secret Labs AB 1997.
  16. # Copyright (c) Fredrik Lundh 1996.
  17. #
  18. # See the README file for information on usage and redistribution.
  19. #
  20.  
  21. import Image, ImageFilter, ImageStat
  22.  
  23. class _Enhance:
  24.  
  25.     ##
  26.     # Returns an enhanced image. The enhancement factor is a floating
  27.     # point value controlling the enhancement. Factor 1.0 always
  28.     # returns a copy of the original image, lower factors mean less
  29.     # colour (brightness, contrast, etc), and higher values more.
  30.     # There are no restrictions on this value.
  31.     #
  32.     # @param factor Enhancement factor.
  33.     # @return An enhanced image.
  34.  
  35.     def enhance(self, factor):
  36.         return Image.blend(self.degenerate, self.image, factor)
  37.  
  38. ##
  39. # Color enhancement object.
  40. # <p>
  41. # This class can be used to adjust the colour balance of an image, in
  42. # a manner similar to the controls on a colour TV set.  An enhancement
  43. # factor of 0.0 gives a black and white image, a factor of 1.0 gives
  44. # the original image.
  45.  
  46. class Color(_Enhance):
  47.     "Adjust image colour balance"
  48.     def __init__(self, image):
  49.         self.image = image
  50.         self.degenerate = image.convert("L").convert(image.mode)
  51.  
  52. ##
  53. # Contrast enhancement object.
  54. # <p>
  55. # This class can be used to control the contrast of an image, similar
  56. # to the contrast control on a TV set.  An enhancement factor of 0.0
  57. # gives a solid grey image, factor 1.0 gives the original image.
  58.  
  59. class Contrast(_Enhance):
  60.     "Adjust image contrast"
  61.     def __init__(self, image):
  62.         self.image = image
  63.         mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
  64.         self.degenerate = Image.new("L", image.size, mean).convert(image.mode)
  65.  
  66. ##
  67. # Brightness enhancement object.
  68. # <p>
  69. # This class can be used to control the brighntess of an image.  An
  70. # enhancement factor of 0.0 gives a black image, factor 1.0 gives the
  71. # original image.
  72.  
  73. class Brightness(_Enhance):
  74.     "Adjust image brightness"
  75.     def __init__(self, image):
  76.         self.image = image
  77.         self.degenerate = Image.new(image.mode, image.size, 0)
  78.  
  79. ##
  80. # Sharpness enhancement object.
  81. # <p>
  82. # This class can be used to adjust the sharpness of an image.  The
  83. # enhancement factor 0.0 gives a blurred image, 1.0 gives the original
  84. # image, and a factor of 2.0 gives a sharpened image.
  85.  
  86. class Sharpness(_Enhance):
  87.     "Adjust image sharpness"
  88.     def __init__(self, image):
  89.         self.image = image
  90.         self.degenerate = image.filter(ImageFilter.SMOOTH)
  91.